package com.slipper.gen.util;

import java.util.Random;

public class Tool {
    /**
     * uuid获取，20位UUID，无，11位16进制时间 + 9位随机字条串
     * return UUID.randomUUID().toString().toLowerCase().replaceAll("-", "");
     * @return
     */
    public static String getUUID() {
        String prefix = fillLength(Long.toHexString(System.currentTimeMillis()), 12, '0');
        return prefix + getRandomHexString(20 - prefix.length());
    }

    public static String getRandomHexString(int length) {
        String str = "0123456789abcdef";
        StringBuilder returnStr = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            returnStr.append(str.charAt(random.nextInt(str.length())));
        }
        return returnStr.toString();
    }

    /**
     * 从左边开始填充字符串至特定长度
     *
     * @param str
     * @param len
     * @param c
     * @return
     */
    public static String fillLength(String str, int len, char c) {
        if (str != null && !str.isEmpty() && str.length() >= len) {
            return str;
        }
        StringBuilder buf = new StringBuilder();
        for (int i = str == null ? 0 : str.length(); i < len; i++) {
            buf.append(c);
        }
        buf.append(str == null ? "" : str);
        return buf.toString();
    }
}
